package com.znzz.controller;

import com.znzz.entity.User;
import com.znzz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

@Controller
@RequestMapping("/course")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public String login(HttpServletRequest request, HttpSession session, HttpServletResponse response) throws ServletException, IOException {
       String uid=request.getParameter("uid");
       String password=request.getParameter("password");
       String remember=request.getParameter("remember");
       System.out.println(uid+password);
       User user=userService.checklogin(uid,password);
        System.out.println(user);
        if(user!=null){
            session.setAttribute("session_user",user);
            return "admin/admin_index";
        }else{
            session.setAttribute("msg","账户或密码错误");
            return "admin/admin_login";
        }
    }

    /**
     * 主页面
     * @return
     */
    @RequestMapping("/MainIndex")
    public String MainIndex(){
        return "admin/admin_index";
    }

    @RequestMapping("/index")
    public String index(HttpServletRequest request, HttpSession session){
        return "course/index";
    }

    @RequestMapping("/loginOut")
    public String loginOut(HttpSession session){
        session.invalidate();
        return "redirect:/admin_login.jsp";
    }

    @RequestMapping("/password")
    public String password(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
        String action=request.getParameter("action");
        //获取当前用户信息
        User userCur=(User)request.getSession().getAttribute("session_user");
        System.out.println("action:"+action+":userCur:"+userCur);


        if(action !=null && action.equals("preChange")){
            //跳转到密码修改页
            return "admin/admin_passwordChange";
        }else if(action !=null && action.equals("ajaxOldPassword")){
            //校验用户输入的原密码是否正确
            //当前登录的用户的输入框输入的密码
            String oldPassWord=request.getParameter("oldPassword");
            System.out.println("oldPassWord:"+oldPassWord);
            //当前登录的用户的原始密码
            String password=userCur.getPassword();
            if(!password.equals(oldPassWord)){
                response.setCharacterEncoding("utf-8");
                PrintWriter writer=response.getWriter();
                writer.print("你输入的原始密码错误：请重新输入！");
            }
        }else if(action !=null && action.equals("change")){
            //修改密码
            String newPassword=request.getParameter("newPassword");
            System.out.println("newPassword:"+newPassword);
            User user=new User();
            System.out.println("userCur.getId():"+userCur.getId());
            user.setId(userCur.getId());
            user.setPassword(newPassword);
            userService.updatePassword(user);
            //跳转到修改密码界面
            request.setAttribute("error","修改密码成功");
            return "admin/admin_passwordChange";
        }
        return null;
    }

}
